package org.itboys.core.controller.front.order;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.core.dro.order.SundayRecycleOrderDro;
import org.itboys.core.dro.order.SundayRecycleOrderTrayDro;
import org.itboys.core.dto.order.SundayRecycleOrderDto;
import org.itboys.core.dto.order.SundaySendOrderDto;
import org.itboys.core.service.mysql.front.SundayFrontRecycleOrderService;
import org.itboys.core.util.AccountIdHoldUtils;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2018/12/11
 * 描述：回收订单
 */
@Controller
@RequestMapping("/sunday/mobile/recycleOrder")
@Api(value = "recycleOrder", description = "回收订单")
public class SundayMobileRecycleOrderController extends BaseController {
    @Autowired
    private SundayFrontRecycleOrderService recycleOrderService;

    @RequestMapping(value = "/findOne", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端,供应商端】订单详情", notes = "订单详情的接口")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单ID") @RequestParam(value = "orderId", required = true) Long orderId
    ) {
        SundayRecycleOrderDto result = recycleOrderService.findOne(orderId);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/select", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端,供应商端】订单列表", notes = "订单列表的接口")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单状态 0,全部 1,待确认2,已确认3,已回收,4,已收盘") @RequestParam(value = "status", required = true) Integer status,
            @ApiParam(value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(value = "排序参数。字段名称。默认为空。") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        List<SundayRecycleOrderDto> result = recycleOrderService.selectOrders(request, memberId, status);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】创建回收订单",
            notes = "sendOrderId:配送订单id recycleDate:回收时间 recycleProvinceId:回收省份id recycleProvince:回收省份 recycleCityId:回收城市id" +
                    "recycleDistrictId:回收区域id recycleDistrict:回收区域 recycleAddress:回收地址 models:订单模板")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "请求参数json", required = true) @RequestBody SundayRecycleOrderDro recycleOrder
    ) throws Exception {
        recycleOrder.validate();
        SundayRecycleOrderDto result = recycleOrderService.createOrder(recycleOrder);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/confirm", method = {RequestMethod.POST})
    @ApiOperation(value = "【供应商端】确认回收订单", notes = "确认回收订单的接口")
    public void confirmOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId
    ){
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundayRecycleOrderDto result = recycleOrderService.confirmOrder(orderId, memberId);
        ResponseMessage.success(result);
    }

    @RequestMapping(value = "/recycle", method = {RequestMethod.POST})
    @ApiOperation(value = "【供应商端】回收订单",
            notes = "orderId:回收订单id harmNum:损坏数量 trayIds:正常托盘ids harmRecords:损坏记录")
    public void recycleOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "请求参数json", required = true) @RequestBody SundayRecycleOrderTrayDro trayDro
    ) throws Exception {
        trayDro.validate();
        SundayRecycleOrderDto result = recycleOrderService.recycleOrder(trayDro);
        ResponseMessage.success(result);
    }

    @RequestMapping(value = "/scan", method = {RequestMethod.POST})
    @ApiOperation(value = "【供应商端】回收订单扫码校检", notes = "回收订单扫码校检的接口")
    public void scan(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId,
            @ApiParam(value = "模板id") @RequestParam(value = "modelId", required = true) Long modelId,
            @ApiParam(value = "托盘id") @RequestParam(value = "trayId", required = true) Long trayId
    ) {
        recycleOrderService.scan(orderId,modelId,trayId);
        ResponseMessage.success();
    }



    @RequestMapping(value = "/receive", method = {RequestMethod.POST})
    @ApiOperation(value = "【供应商端】回收订单收盘", notes = "回收订单收盘的接口")
    public void receiveOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId
    ){
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundayRecycleOrderDto result = recycleOrderService.receiveOrder(orderId,memberId);
        ResponseMessage.success(result);
    }


}
